package uestc.lj.gulishop.product.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;
import uestc.lj.common.utils.PageUtils;
import uestc.lj.common.utils.R;
import uestc.lj.gulishop.product.entity.ProductAttrValueEntity;
import uestc.lj.gulishop.product.service.AttrService;
import uestc.lj.gulishop.product.service.ProductAttrValueService;
import uestc.lj.gulishop.product.vo.AttrResponseVO;
import uestc.lj.gulishop.product.vo.AttrVO;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 商品属性
 *
 * @author crazlee
 * @email peterlee0@163.com
 * @date 2021-05-21 18:45:10
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    @Autowired
    private ProductAttrValueService productAttrValueService;

    /**
     * 根据spuid修改规格中的属性值
     *
     * @param spuId
     * @return
     */
    @PostMapping("/update/{spuId}")
    public R updateSpuAtrr(@PathVariable("spuId") Long spuId,
                           @RequestBody List<ProductAttrValueEntity> productAttrValueEntities) {
        productAttrValueService.updateSpuAtrr(spuId, productAttrValueEntities);
        return R.ok();
    }

    /**
     * 根据spuid查询spu规格参数属性，做回显使用
     *
     * @param spuId
     * @return
     */
    @GetMapping("/base/listforspu/{spuId}")
    public R baseAttrListForSpu(@PathVariable("spuId") Long spuId) {
        List<ProductAttrValueEntity> productAttrValueEntities = productAttrValueService.baseAttrListForSpu(spuId);
        return R.ok().put("data", productAttrValueEntities);
    }

    /**
     * 查询分类的规格参数
     *
     * @return
     */
    @GetMapping("/{attrType}/list/{catelogId}")
    public R baseAttrList(@RequestParam Map<String, Object> params,
                          @PathVariable("catelogId") Long catelogId,
                          @PathVariable("attrType") String type) {
        PageUtils page = attrService.queryBaseAttrPage(params, catelogId, type);
        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    // @RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @Cacheable(value = "attr", key = "'attrInfo:'+#root.args[0]")
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId) {
        // AttrEntity attr = attrService.getById(attrId);

        AttrResponseVO attrResponseVO = attrService.getAttrInfo(attrId);
        return R.ok().put("attr", attrResponseVO);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //  @RequiresPermissions("product:attr:save")
    public R save(@RequestBody AttrVO attr) {
        attrService.saveAttr(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //  @RequiresPermissions("product:attr:update")
    public R update(@RequestBody AttrVO attr) {
        attrService.updateAttr(attr);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds) {
        attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
